package cn.zzuli.shiyan5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    String path = "https://www.wanandroid.com/article/list/0/json";
    ListView listView;
    private List<Article> articleList = new ArrayList<>();
    MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.lv);
        myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
        new Thread() {
            public void run() {
                String jsonString = getJsonString(path);
                articleList = parseJson(jsonString);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myAdapter.notifyDataSetChanged();
                    }
                });
            }

        }.start();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, InputActivity.class);
                intent.putExtra("link", articleList.get(position).getLink());
                startActivity(intent);
            }
        });

    }

    private String getJsonString(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null) {
                buffer.append(line);

            }
            return buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private List<Article> parseJson(String jsonString) {
        List<Article> articles = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(jsonString);
            int code = object.optInt("errorcode");
            if (code != 0) {

                return null;
            }
            JSONObject object1 = object.getJSONObject("data");
            JSONArray datas = object1.optJSONArray("datas");
            for (int i = 0; i < datas.length(); i++) {

                JSONObject object2 = datas.getJSONObject(i);
                String title = object2.optString("title");
                String author = object2.optString("author");
                String link = object2.optString("link");
                Article article = new Article(title, author, link);
                articles.add(article);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return articles;
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return articleList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, parent, false);
                holder = new ViewHolder();
                holder.author = (TextView) convertView.findViewById(R.id.tx_author);
                holder.title = (TextView) convertView.findViewById(R.id.tx_title);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();

            }
            holder.title.setText("标题:" + articleList.get(position).getTitle());
            holder.author.setText("作者:" + articleList.get(position).getAuthor());
            return convertView;

        }

        class ViewHolder {
            TextView author;
            TextView title;
        }
    }


}